<?php

namespace app\admin\controller;
use library\Controller;
use \tp5er\Backup;
/**
 * 数据库备份管理
 * Class Database
 * @package app\admin\controller
 */
class Database extends Controller
{
    //数据库备份配置
    private function backupsConfig(){
        return [
            'path'=>app()->getRootPath().'backup/'
        ];
    }
    /**
     * 数据库备份列表
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/9/27 17:32
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        if($this->request->isPost()){
            foreach ($this->request->post() as $k => $v) sysconf($k, $v);
            $this->success('数据配置保存成功');
        }
        $this->title = '数据库备份管理';
        $db = new Backup($this->backupsConfig());
        $sv = $db->fileList();

        $leng = count($sv);
        return $this->fetch('', ['list' => $sv, 'title' => $this->title, 'leng' => $leng]);
    }


    /**
     *  添加数据备份
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/9/27 17:33
     * @throws \Exception
     */
    public function addBackups()
    {
        $time = time();
        $file = ['name' => date('Ymd-His', $time), 'part' => 1];
        //获取数据表列表
        $db = new Backup($this->backupsConfig());
        set_time_limit(0);
        $tableList = $db->dataList();
        $is_del = false;
        foreach ($tableList as $k => $v) {
            $start = $db->setFile($file)->backup($v['name'], 0);
            if ($start == 0) {
                continue;
            } else {
                $is_del = true;
                break;
            }
        }
        if ($is_del) {
            //备份失败
            $db->delFile($time);
            $this->error('备份失败!请稍后再试');
        } else {
            $this->success('备份成功');
        }
    }


    /**
     *  数据备份还原
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/9/27 17:33
     * @throws \Exception
     */
    public function restoreBackups()
    {
        $time = $this->request->get('time');
        $db = new Backup($this->backupsConfig());

        $file = ['name' => date('Ymd-His', $time),'part'=>1];
        $res = $db->setTimeout(0)->setFile($file)->import(0);
        $res ? $this->success('还原成功') : $this->error('还原失败');
    }


    /**
     * 删除数据备份
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/9/27 17:33
     * @throws \Exception
     */
    public function delBackups()
    {
        if($this->request->isPost()){
            $post = $this->request->post();
            foreach (explode(',',$post['id']) as $key => $value) {
                $db = new Backup($this->backupsConfig());
                $db->delFile($value);
            }
            $this->success("备份删除成功！", '');
        }
    }


    /**
     * 下载备份
     * @Author: rocky
     * @auth true
     * @menu true
     * 2019/9/27 17:33
     * @throws \Exception
     */
    public function downloadBackups()
    {
        $time = $this->request->get('time');
        $db = new Backup($this->backupsConfig());
        $db->downloadFile($time);
    }


    /**
     * 自动备份调用方法
     * @auth true
     * @menu true
     * @Author: rocky
     * 2019/9/27 17:33
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function autoBackups(){
        if(sysconf('database_is_auto') == 0) return false;
        $db = new Backup($this->backupsConfig());
        $sv = $db->fileList();

        $count = count($sv);
        if($count > 0){
            if(time()<($sv[0]['time']+86400*sysconf('database_day'))){
                return false;
            }
            if($count>sysconf('database_number')){
                $db->delFile($sv[$count-1]['time']);
            }
        }
        $time = time();
        $file = ['name' => date('Ymd-His', $time), 'part' => 1];
        //获取数据表列表
        $db = new Backup($this->backupsConfig());
        set_time_limit(0);
        $tableList = $db->dataList();
        $is_del = false;
        foreach ($tableList as $k => $v) {
            $start = $db->setFile($file)->backup($v['name'], 0);
            if ($start == 0) {
                continue;
            } else {
                $is_del = true;
                break;
            }
        }
        if ($is_del) {
            //备份失败
            $db->delFile($time);
            echo 'Backups error';
        } else {
            echo 'backups success';
        }
    }
}